﻿using clf.weighing.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using RadioButton = System.Windows.Controls.RadioButton;

namespace weighing
{
    /// <summary>
    ///标准版过磅 的交互逻辑
    /// </summary>
    public partial class NomalWeighingUc : System.Windows.Controls.UserControl
    {
        public NomalWeighingUc()
        {
            InitializeComponent();
        }

        #region event

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //this.MainGrid.Height = this.DesiredSize.Height;
            if (this.IsLoaded)
            {
                UpdateSize();
            }
        }

        #endregion

        #region Main menum click update ui

        public void RelayoutByMenumItem(string miName)
        {
            switch (miName)
            {
                case nameof(MenuItemName.VideoAreaMI):
                    //在其它地方已经处理
                    break;
                case nameof(MenuItemName.InWeighingMI):
                    if (InWeighingRb.Visibility == Visibility.Visible)
                    {
                        InWeighingRb.IsChecked = false;
                        InWeighingRb.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        InWeighingRb.Visibility = Visibility.Visible;
                        InWeighingRb.IsChecked = true;
                    }
                    break;
                case nameof(MenuItemName.InDataMI):
                    if (inputDataRb.Visibility == Visibility.Visible)
                    {
                        inputDataRb.IsChecked = false;
                        inputDataRb.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        inputDataRb.Visibility = Visibility.Visible;
                        inputDataRb.IsChecked = true;
                    }
                    break;
                case nameof(MenuItemName.OutWeighingMI):
                    if (OutWeighingRb.Visibility == Visibility.Visible)
                    {
                        OutWeighingRb.IsChecked = false;
                        OutWeighingRb.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        OutWeighingRb.Visibility = Visibility.Visible;
                        OutWeighingRb.IsChecked = true;
                    }
                    break;
                case nameof(MenuItemName.OutDataMI):
                    if (outputDataRb.Visibility == Visibility.Visible)
                    {
                        outputDataRb.IsChecked = false;
                        outputDataRb.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        outputDataRb.Visibility = Visibility.Visible;
                        outputDataRb.IsChecked = true;
                    }
                    break;
                default:

                    break;
            }
        }


        #endregion

        private bool isUpdateSizing = false;

        private void UpdateSize()
        {
            if (isUpdateSizing) { return; }
            isUpdateSizing = true;

            this.optionRowDefinition.Height = GridLength.Auto;

            var buttomAreaAh = this.buttomArea.ActualHeight;
            var ah = this.mainPanel.ActualHeight;
            if (buttomAreaAh <= ah && buttomAreaAh <= 400)
            {
                isUpdateSizing = false;
                return;
            }
            if (this.InputPanel.Children.Count == 2 && this.InputPanel.Visibility == Visibility.Visible)
            {
                this.InputPanel.Height = this.mainPanel.ActualHeight;
                var child1 = this.InputPanel.Children[0] as FrameworkElement;
                var child2 = this.InputPanel.Children[1] as FrameworkElement;
                double height = this.InputPanel.ActualHeight - (child1?.ActualHeight??0) - 4;
                if (child2!= null && height > 0)
                {
                    child2.Height = height;
                }
            }
            else
            {
                if (this.InputDataPanel.Visibility == Visibility.Visible)
                {
                    var bah = this.buttomArea.ActualHeight;
                    var mah = this.mainPanel.ActualHeight;
                    var h = bah > mah && bah > 400 ? bah : mah;
                    this.InputDataUc.Height = h;
                }
            }

            if (this.OutputPanel.Children.Count == 2 && this.OutputPanel.Visibility == Visibility.Visible)
            {
                this.OutputPanel.Height = this.mainPanel.ActualHeight;
                var child1 = this.OutputPanel.Children[0] as FrameworkElement;
                var child2 = this.OutputPanel.Children[1] as FrameworkElement;
                var height = this.OutputPanel.ActualHeight - (child1?.ActualHeight ?? 0) - 4;
                if (child2 != null && height > 0)
                {
                    child2.Height = height;
                }
            }
            else
            {
                if (this.OutputDataPanel.Visibility == Visibility.Visible)
                {
                    var bah = this.buttomArea.ActualHeight;
                    var mah = this.mainPanel.ActualHeight;
                    var h = bah > mah && bah > 400 ? bah : mah;
                    this.outputDataUc.Height = h;
                }
            }
            isUpdateSizing = false;
        }


        /// <summary>
        /// 重先布局
        /// </summary>
        public void ReLayout()
        {
            if (App.Config.UserConfig.ShowVideoArea)
            {
                if (App.Config.UserConfig.ShowInWeighing)
                {
                    if (InputPanel.Children.Count > 1)
                    {
                        InputPanel.Children.RemoveAt(1);
                    }
                    if (App.Config.UserConfig.ShowInData)
                    {
                        inputDataRb.Visibility = Visibility.Visible;
                        InputDataUc.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        inputDataRb.IsChecked = false;
                        inputDataRb.Visibility = Visibility.Collapsed;
                    }
                }
                if (App.Config.UserConfig.ShowOutWeighing)
                {
                    if (OutputPanel.Children.Count > 1)
                    {
                        OutputPanel.Children.RemoveAt(1);
                    }
                    if (App.Config.UserConfig.ShowOutData)
                    {
                        outputDataRb.Visibility = Visibility.Visible;
                        outputDataUc.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        outputDataRb.IsChecked = false;
                        outputDataRb.Visibility = Visibility.Collapsed;
                    }
                }
            }
            else
            {
                //video don't show
                if (InputPanel.Children.Count == 1)
                {
                    inputDataRb.IsChecked = false;
                    inputDataRb.Visibility = Visibility.Collapsed;
                    var iuc = new InputDataContent() { MinHeight = 350 };
                    InputPanel.Children.Add(iuc);
                }

                if (OutputPanel.Children.Count == 1)
                {
                    outputDataRb.IsChecked = false;
                    outputDataRb.Visibility = Visibility.Collapsed;
                    var ouc = new OutputDataContent() { MinHeight = 350 };
                    OutputPanel.Children.Add(ouc);
                }
            }
            //default input
            SelectDefault();
        }


        /// <summary>
        ///  选择一个默认界面
        /// </summary>
        public void SelectDefault()
        {
            bool hasSelected = false;
            for (int i = 0; i < RbPanel.Children.Count; i++)
            {
                var rb = RbPanel.Children[0] as RadioButton;
                if (rb != null && rb.IsChecked == true)
                {
                    hasSelected = true;
                    break;
                }
            }
            if (hasSelected == false && RbPanel.Children.Count > 0)
            {
                var rb = RbPanel.Children[0] as RadioButton;
                if (rb != null && (rb.Visibility == Visibility.Visible || rb.IsVisible == true))
                    rb.IsChecked = true;
            }
        }


        /// <summary>
        /// 有未未完成数据的处理
        /// </summary>
        public void HasNoFinishHandle(WeighingBill noFinished)
        {
            if (noFinished.type == (int)WeightingBillType.Buy)
            {
                this.InWeighingRb.IsChecked = true;
                this.InputWeighingUc.OnCarSelected(noFinished);
                this.InputWeighingUc.IsRecongnizePlateWeighing = true;
                this.InputWeighingUc.outFactoryRb.IsChecked = true;
                this.InputWeighingUc.IsRecongnizePlateWeighing = false;
                this.InputWeighingUc.ShowDecuationWeightView();
            }
            else if (noFinished.type == (int)WeightingBillType.Sale)
            {
                this.OutWeighingRb.IsChecked = true;
                this.outputWeighingUc.OnCarSelected(noFinished);
                this.outputWeighingUc.IsRecongnizePlateWeighing = true;
                this.outputWeighingUc.outFactoryRb.IsChecked = true;
                this.outputWeighingUc.IsRecongnizePlateWeighing = false;
            }
        }


    }
}
